
package com.tanker.common.utils.microRental.common; 

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tanker.common.utils.StringUtil;
import com.tanker.common.utils.microRental.APIClient;

public class RespResult<T> implements Serializable{
	private final static Logger logger = LoggerFactory.getLogger(RespResult.class);
	private static final long serialVersionUID = -4197055428740584325L;
	private RespStatus status;
	private T data;
	private List<T> datas;
	
	public RespResult(){}
	
	/**
	 * @param jsonStr 微租车json 串
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public void format(String jsonStr, Class clazz) throws Exception {
		if(StringUtil.nullOrBlank(jsonStr)) {
			logger.error("jsonStr is null!");
			return;
		}

		try {
			JSONObject obj = JSONObject.fromObject(jsonStr);
			if(obj!=null) {
				JSONObject jsonStatus = obj.getJSONObject("status");
				if(jsonStatus!=null) {
					@SuppressWarnings("unchecked")
					RespStatus rs = (RespStatus)JSONObject.toBean(jsonStatus, RespStatus.class);
					setStatus(rs);//设置返回状态
					if(clazz==null)
						return;
					Object jsonObj = obj.get("data");
					if(jsonObj==null)
						return;
					if(jsonObj instanceof JSONObject) {
						JSONObject jsonData = (JSONObject)obj.getJSONObject("data");
						
						@SuppressWarnings("unchecked")
						T t = (T)JSONObject.toBean(jsonData, clazz);
						setData(t);//设置data
					} else if(jsonObj instanceof JSONArray) {//返回list
						JSONArray jsonArray = (JSONArray)obj.getJSONArray("data");
						
						if(jsonArray!=null && jsonArray.size()>0) {
							datas = new ArrayList<T>();
							setDatas(datas);//设置datas
							for(int i=0; i<jsonArray.size();i++) {
								JSONObject bean = jsonArray.getJSONObject(i);
								if(bean!=null) {
									@SuppressWarnings("unchecked")
									T t = (T)JSONObject.toBean(bean, clazz);
									datas.add(t);
								}
							}
						}
					}
				}
			}
		} catch(Exception e) {
			logger.error("json转化出错....", e);
		}
	}
	
	/**
	 * @param map 参数map
	 * @param url
	 * @param clazz
	 */
	public void resolve(Map<String, String> map, String url, Class clazz) {
		APIClient client = new APIClient();
		String ret = client.post(map, url);
	
		if(StringUtil.nullOrBlank(ret)) {
			logger.error("微租车接口返回数据 is null!");
			return;
		}
		logger.error("微租车接口返回数据 url={} map={}结果ret={}",url,map,ret);

		String returnSign = ret.substring(ret.length() - 28);
		String noSignResult = ret.substring(0, ret.length() - 28);

		boolean check = SignHelp.validateSign(noSignResult, returnSign, ConfUtil.privateKey);
		
		logger.info("return sign validate: check=" + check);
		if(!check) {
			logger.error("微租车接口返回数据验证不通过!");
			return;
		}
		
		
		try {
			format(noSignResult, clazz);
		} catch(Exception e) {
			logger.error("微租车接口返回数据解析json出错!", e);
		}
	}

	public RespStatus getStatus() {
		return status;
	}

	public void setStatus(RespStatus status) {
		this.status = status;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public List<T> getDatas() {
		return datas;
	}

	public void setDatas(List<T> datas) {
		this.datas = datas;
	}

}
